﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.IDAL;
using OnlineManagement.DBUtility;
namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
	/// 数据访问类:PigPen
	/// </summary>
	public partial class PigPen:IPigPen
	{
		public PigPen()
		{}
		#region  BasicMethod

		
		public bool Exists(int customerId,int hoggerId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PigPen");
            strSql.Append(" where CustomizeID=@customerId and HoggeryID=@hoggerId ");
			SqlParameter[] parameters = {
					new SqlParameter("@customerId", SqlDbType.SmallInt,2),
                    new SqlParameter("@hoggerId",SqlDbType.Int,4)};
            parameters[0].Value = customerId;
            parameters[1].Value = hoggerId;
			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        public System.Collections.Generic.List<Model.PigPen> GetAllPigPens(int hoggeryId)
        {
            System.Collections.Generic.List<Model.PigPen> lists = new System.Collections.Generic.List<Model.PigPen>();
            SqlDataReader reader=null;
            try
            {
                IDataParameter[] parameters = new IDataParameter[] { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
                //SqlParameter[] parameters = new SqlParameter[] { new SqlParameter { ParameterName = "@hoggeryId",Value=hoggeryId } };
                parameters[0].Value = hoggeryId;

                reader = DbHelperSQL.RunProcedure(Common.PubProcedures.GetAllPigPensProce, parameters);
                //{
                Model.PigPen pp;
                while (reader.Read())
                {
                    pp = new Model.PigPen();
                    pp.HoggeryID = hoggeryId;
                    pp.CustomizeID = string.IsNullOrEmpty(reader[1].ToString()) ? Common.PubConstant.EmptyString : reader[1].ToString();
                    int count = 0;
                    int.TryParse(reader[2].ToString(), out count);
                    pp.CurrentPigCount = count;

                    int.TryParse(reader[3].ToString(),out count);
                    pp.FeederID = count;
                    pp.FeederName = string.IsNullOrEmpty(reader[4].ToString()) ? Common.PubConstant.EmptyString : reader[4].ToString();
                    pp.PigPenID =Guid.Parse(reader[5].ToString());
                    
                    lists.Add(pp);
                }
                // }
                return lists;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {
                if (reader != null) { reader.Close(); }
            }
           
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(OnlineManagement.Model.PigPen model, string feederName, out string returnValue)
		{
            returnValue = "";
            int rowsAffected;
            if (model == null)
            {
                return false;
            }
            
            SqlParameter[] parameters = {
					new SqlParameter("@hoggerId", SqlDbType.Int,4),
					new SqlParameter("@customizeID", SqlDbType.NVarChar,50),
					new SqlParameter("@maxCapacity", SqlDbType.TinyInt,1),
					new SqlParameter("@currentPigCount", SqlDbType.TinyInt,1),
					new SqlParameter("@currentPigType", SqlDbType.NVarChar,20),
					new SqlParameter("@currentPigBit", SqlDbType.NVarChar,20),
					new SqlParameter("@pigPenDescription", SqlDbType.NVarChar,200),
                    new SqlParameter("@feederName",SqlDbType.NVarChar,50),
                    new SqlParameter("@msg",SqlDbType.NVarChar,100),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1)
                                        };
			//parameters[0].Value = Guid.NewGuid();
			parameters[0].Value = model.HoggeryID;
			parameters[1].Value = model.CustomizeID;
			parameters[2].Value = model.MaxCapacity;
			parameters[3].Value = model.CurrentPigCount;
			parameters[4].Value = model.CurrentPigType;
			parameters[5].Value = model.CurrentPigBit;
			parameters[6].Value = model.PigPenDescription;
            parameters[7].Value = feederName;
            parameters[8].Direction = ParameterDirection.Output;
            parameters[9].Direction = ParameterDirection.Output;
            DbHelperSQL.RunProcedure(Common.PubProcedures.PigPenAddproce, parameters, out rowsAffected, out returnValue);
            return rowsAffected > 0 ? true : false;
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OnlineManagement.Model.PigPen model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PigPen set ");
			strSql.Append("HoggeryID=@HoggeryID,");
			strSql.Append("CustomizeID=@CustomizeID,");
			strSql.Append("MaxCapacity=@MaxCapacity,");
			strSql.Append("CurrentPigCount=@CurrentPigCount,");
			strSql.Append("CurrentPigType=@CurrentPigType,");
			strSql.Append("CurrentPigBit=@CurrentPigBit,");
			strSql.Append("PigPenDescription=@PigPenDescription");
			strSql.Append(" where PigPenID=@PigPenID ");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					new SqlParameter("@CustomizeID", SqlDbType.SmallInt,2),
					new SqlParameter("@MaxCapacity", SqlDbType.TinyInt,1),
					new SqlParameter("@CurrentPigCount", SqlDbType.TinyInt,1),
					new SqlParameter("@CurrentPigType", SqlDbType.NVarChar,20),
					new SqlParameter("@CurrentPigBit", SqlDbType.NVarChar,20),
					new SqlParameter("@PigPenDescription", SqlDbType.NVarChar,200),
					new SqlParameter("@PigPenID", SqlDbType.UniqueIdentifier,16)
                   
                                        };
			parameters[0].Value = model.HoggeryID;
			parameters[1].Value = model.CustomizeID;
			parameters[2].Value = model.MaxCapacity;
			parameters[3].Value = model.CurrentPigCount;
			parameters[4].Value = model.CurrentPigType;
			parameters[5].Value = model.CurrentPigBit;
			parameters[6].Value = model.PigPenDescription;
			parameters[7].Value = model.PigPenID;

         
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid pigPenID, out string message)
		{
            message = string.Empty;
            string proceName = Common.PubProcedures.PigPenDeleteProce;
            SqlParameter[] paras = {  new SqlParameter("@pigPenId", SqlDbType.UniqueIdentifier, 16),
                                            new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                                            new SqlParameter("@msg",SqlDbType.NVarChar,200)
                                   };
            paras[0].Value = pigPenID;
            paras[1].Direction = ParameterDirection.Output;
            paras[2].Direction = ParameterDirection.Output;
            int rowsAffected;
            try
            {
                DbHelperSQL.RunProcedure(proceName, paras, out rowsAffected, out message);
                return rowsAffected > 0 ? true : false;
            }catch(Exception ex){
                throw ex;
            }
		} 

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OnlineManagement.Model.PigPen DataRowToModel(DataRow row)
		{
			OnlineManagement.Model.PigPen model=new OnlineManagement.Model.PigPen();
			if (row != null)
			{
				if(row["PigPenID"]!=null && row["PigPenID"].ToString()!="")
				{
					model.PigPenID= new Guid(row["PigPenID"].ToString());
				}
				if(row["HoggeryID"]!=null && row["HoggeryID"].ToString()!="")
				{
					model.HoggeryID=int.Parse(row["HoggeryID"].ToString());
				}
				if(row["CustomizeID"]!=null && row["CustomizeID"].ToString()!="")
				{
					model.CustomizeID=row["CustomizeID"].ToString();
				}
				if(row["MaxCapacity"]!=null && row["MaxCapacity"].ToString()!="")
				{
					model.MaxCapacity=int.Parse(row["MaxCapacity"].ToString());
				}
				if(row["CurrentPigCount"]!=null && row["CurrentPigCount"].ToString()!="")
				{
					model.CurrentPigCount=int.Parse(row["CurrentPigCount"].ToString());
				}
				if(row["CurrentPigType"]!=null)
				{
					model.CurrentPigType=row["CurrentPigType"].ToString();
				}
				if(row["CurrentPigBit"]!=null)
				{
					model.CurrentPigBit=row["CurrentPigBit"].ToString();
				}
				if(row["PigPenDescription"]!=null)
				{
					model.PigPenDescription=row["PigPenDescription"].ToString();
				}
			}
			return model;
		}

		#endregion  BasicMethod
		#region  ExtensionMethod

        public DataSet GetAvailabltePigPens(int hoggeryId) {
            try
            {
                string query = "select PigPenID,CustomizeID from PigPen where CurrentPigCount=0 and CurrentPigBit is null and HoggeryID=@hoggeryId order by CustomizeID";
                SqlParameter[] paras = { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
                paras[0].Value = hoggeryId;
                DataSet ds = DbHelperSQL.Query(query, paras);
                return ds;
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// pigpens paging
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="strWhere"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="iscount"></param>
        /// <param name="rCount"></param>
        /// <returns></returns>
        public DataSet PigPensByPaging(
            int hoggeryId,string strWhere,string strOrderBy,
            int pageIndex,int pageSize,out int rCount) {
            DataSet ds = null;
            rCount = 0;
            string proceName = Common.PubProcedures.PigPensByPagingProce;
            SqlParameter[] paras = { 
                                     new SqlParameter("@hoggeryId", SqlDbType.Int, 4),
                                   new SqlParameter("@Where",SqlDbType.NVarChar,500),
                                   new SqlParameter("@OrderBy",SqlDbType.NVarChar,500),
                                   new SqlParameter("@PageIndex",SqlDbType.Int,4),
                                   new SqlParameter("@PageSize",SqlDbType.Int,4),
                                   new SqlParameter("@IsCount",SqlDbType.Bit)
                                   };
            paras[0].Value = hoggeryId;
            paras[1].Value = strWhere;
            paras[2].Value = strOrderBy;
            paras[3].Value = pageIndex;
            paras[4].Value = pageSize;
            paras[5].Value = 1;
            try
            {
                ds = DbHelperSQL.RunProcedure(proceName, paras);
                if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
                {
                    string str = ds.Tables[0].Rows[0][0].ToString();
                    int.TryParse(str, out rCount);
                }
                
            }catch(Exception ex){
                throw ex;
            }
            return ds;
        }
		#endregion  ExtensionMethod


       
    }
}

